package controllers.power.admin;

import controllers.BaseController;
import controllers.auth.Secure;
import helper.GlobalConfig;
import models.admin.AdminMenu;
import models.admin.AdminMenuItem;
import models.admin.AdminRole;
import models.constants.AvailableStatus;
import org.apache.commons.lang.StringUtils;
import play.modules.paginate.JPAExtPaginator;
import play.mvc.Controller;
import play.mvc.With;
import util.common.ConvertUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 菜单管理
 * Created by youliangcheng on 17/4/8.
 */
@With(BaseController.class)
public class AdminMenuCtrl extends Controller {
    public static void loadMenuList(Integer pageNumber,
                                Long id,
                                String code,
                                String name,
                                String status) {
        pageNumber = pageNumber == null ? 1 : pageNumber;
        Map<String, Object> searchMap = new HashMap<>();
        if (id != null)
            searchMap.put("id", id);

        if (StringUtils.isNotBlank(code))
            searchMap.put("code", "%" + code + "%");

        if (StringUtils.isNotBlank(name))
            searchMap.put("name", "%" + name + "%");

        if (StringUtils.isNotBlank(status))
            searchMap.put("status", status);

        JPAExtPaginator<AdminMenu> resultPage = AdminMenu.findByCondition(searchMap, "id desc", pageNumber, GlobalConfig.PAGE_SIZE);
        render(resultPage, pageNumber, code,name);
    }


    //禁用
    public static void freezeMenu(long id , Integer pageNumber) {

        AdminMenu adminMenu = AdminMenu.findById(id);
        if (adminMenu.status == AvailableStatus.FREEZE) {
            flash.put("msg", "该菜单已经被禁用,无需重复操作!");
            redirect("/power/admin/AdminMenuCtrl/loadMenuList/" + pageNumber);
        }

        //判断菜单是否被使用
        if(AdminRole.countByMenu(id) > 0){
            flash.put("msg", "该菜单正在被使用,不能禁用!");
            redirect("/power/admin/AdminMenuCtrl/loadMenuList/" + pageNumber);
        }

        adminMenu.status = AvailableStatus.FREEZE;
        adminMenu.save();
        flash.put("msg", "禁用成功");
        redirect("/power/admin/AdminMenuCtrl/loadMenuList/" + pageNumber);
    }

    //启用
    public static void availableMenu(long id , Integer pageNumber) {

        AdminMenu adminMenu = AdminMenu.findById(id);
        if (adminMenu.status == AvailableStatus.AVAILABLE){
            flash.put("msg", "该菜单已经是启用状态,无需重复操作!");
            redirect("/power/admin/AdminMenuCtrl/loadMenuList/" + pageNumber);
        }
        adminMenu.status = AvailableStatus.AVAILABLE;
        adminMenu.save();
        flash.put("msg", "启用成功");
        redirect("/power/admin/AdminMenuCtrl/loadMenuList/" + pageNumber);
    }


    /**************************************重写菜单详情页***********************************/

    //跳转详情页
    public static void loadMenuDetail(long id , Integer pageNumber){

        pageNumber = pageNumber== null ? 1: pageNumber;
        AdminMenu adminMenu = new AdminMenu();

        if(id > 0){
            adminMenu = AdminMenu.findById(id);
        }
        render(adminMenu ,pageNumber);
    }

    //加载树形结构数据,默认先加载1层
    public static void loadMenuItemList(long menuId , long parentId ){
        List<AdminMenuItem> menuItemList = new ArrayList<>();
        if(menuId > 0 ){
            menuItemList = AdminMenuItem.loadChildMenuItem(menuId, parentId);
        }
        renderJSON(ConvertUtil.toJson(menuItemList));
    }

    //保存菜单
    public static void saveDetail(AdminMenu adminMenu, Integer pageNumber) {

        if (adminMenu.id != null && adminMenu.id > 0) {
            Secure.setBaseInfo(adminMenu);
            adminMenu.update(adminMenu);
        } else {
            //自动生成编号
            adminMenu.loadKey();
            //设置创建人和修改人信息
            Secure.setBaseInfo(adminMenu);
            adminMenu.save();
        }
        redirect("/power/admin/AdminMenuCtrl/loadMenuDetail/" + adminMenu.id + "/" + pageNumber);
    }

    //保存菜单明细
    public static void saveAdminMenuItem(AdminMenuItem adminMenuItem , Integer pageNumber){

        if(adminMenuItem != null && adminMenuItem.id != null){ //update
            Secure.setBaseInfo(adminMenuItem);
            adminMenuItem.update(adminMenuItem);
            flash.put("msg","保存成功!");
        }else if(adminMenuItem != null) { //new

            //判断父亲节点
            AdminMenuItem parentMenuItem = AdminMenuItem.findById(adminMenuItem.parentId);
            if(parentMenuItem != null && parentMenuItem.business != null){
                flash.put("msg","父节点包含业务菜单,无法添加子菜单!");
                redirect("/power/admin/AdminMenuCtrl/loadMenuDetail/"+adminMenuItem.menuId+"/"+pageNumber);
            }
            Secure.setBaseInfo(adminMenuItem);
            adminMenuItem.isLeaf = true;
            adminMenuItem.save();

            //判断父亲节点
            if(parentMenuItem != null && parentMenuItem.isLeaf == true){
                parentMenuItem.isLeaf = false;
                parentMenuItem.save();
            }
            flash.put("msg","保存成功!");
        } else {
            flash.put("msg","数据不能为空 , 保存失败!");
        }

        redirect("/power/admin/AdminMenuCtrl/loadMenuDetail/"+adminMenuItem.menuId+"/"+pageNumber);
    }

    //删除菜单明细
    public static void deletedAdminMenuItem(long menuItemId  , Integer pageNumber , long menuId){
        AdminMenuItem adminMenuItem = AdminMenuItem.finbByMenuItemId(menuItemId);

        if(adminMenuItem == null){
            flash.put("msg","数据不存在,可能已经被删除!");
            redirect("/power/admin/AdminMenuCtrl/loadMenuDetail/" + menuId + "/" + pageNumber);
        }

        Long parentId = adminMenuItem.parentId != null ? adminMenuItem.parentId : -1;

        AdminMenuItem.delete("id = ?1 ", menuItemId);

        //判断是否有兄弟节点,如果没有,则将父节点设置为叶子节点
        if (adminMenuItem.loadBrotherQty(parentId) == 0) {

            AdminMenuItem parentMenuItem = AdminMenuItem.findById(adminMenuItem.parentId);
            if (parentMenuItem != null) {
                parentMenuItem.isLeaf = true;
                parentMenuItem.save();
            }
        }

        flash.put("msg", "删除成功!");
        redirect("/power/admin/AdminMenuCtrl/loadMenuDetail/" + menuId + "/" + pageNumber);


    }


}
